# RUN: yaml2obj %s -o %t

## Test that llvm-objcopy adds a section to the given object with expected
## contents.
# RUN: echo DEADBEEF > %t.sec
# RUN: llvm-objcopy --add-section=.test.section=%t.sec %t %t1
# RUN: llvm-readobj --file-headers --sections --section-data %t1 | FileCheck %s --check-prefixes=CHECK-ADD

# CHECK-ADD:      SectionCount: 2
# CHECK-ADD:      Name: .text
# CHECK-ADD:      Name: .test.section
# CHECK-ADD:      Characteristics [
# CHECK-ADD-NEXT:   IMAGE_SCN_ALIGN_1BYTES
# CHECK-ADD-NEXT:   IMAGE_SCN_CNT_INITIALIZED_DATA
# CHECK-ADD-NEXT: ]
# CHECK-ADD:      SectionData (
# CHECK-ADD-NEXT:  0000: {{.+}}|DEADBEEF{{.+}}|
# CHECK-ADD-NEXT: )

## Test that llvm-objcopy can add a section with an empty name.
# RUN: llvm-objcopy --add-section==%t.sec %t %t1.empty.name
# RUN: llvm-readobj --file-headers --sections --section-data %t1.empty.name | FileCheck %s --check-prefixes=CHECK-ADD-EMPTY-NAME

# CHECK-ADD-EMPTY-NAME:      SectionCount: 2
# CHECK-ADD-EMPTY-NAME:      Name: .text
# CHECK-ADD-EMPTY-NAME:      Name: (00 00 00 00 00 00 00 00)
# CHECK-ADD-EMPTY-NAME:      Characteristics [
# CHECK-ADD-EMPTY-NAME-NEXT:   IMAGE_SCN_ALIGN_1BYTES
# CHECK-ADD-EMPTY-NAME-NEXT:   IMAGE_SCN_CNT_INITIALIZED_DATA
# CHECK-ADD-EMPTY-NAME-NEXT: ]
# CHECK-ADD-EMPTY-NAME:      SectionData (
# CHECK-ADD-EMPTY-NAME-NEXT:  0000: {{.+}}|DEADBEEF{{.+}}|
# CHECK-ADD-EMPTY-NAME-NEXT: )

## Test that llvm-objcopy can add a section to an object with extended
## relocations.
# RUN: %python %p/../Inputs/ungzip.py %p/Inputs/x86_64-obj-xrelocs.yaml.gz > %t.xrelocs.yaml
# RUN: yaml2obj %t.xrelocs.yaml -o %t.xrelocs.obj
# RUN: llvm-objcopy --add-section=.test.section=%t.sec %t.xrelocs.obj %t1.xrelocs.obj
# RUN: llvm-readobj --file-headers --sections --section-data %t1.xrelocs.obj | FileCheck %s --check-prefixes=CHECK-EXTENDED-RELOCS

# CHECK-EXTENDED-RELOCS:      SectionCount: 2
# CHECK-EXTENDED-RELOCS:      Name: .data
# CHECK-EXTENDED-RELOCS:      RelocationCount: 65535
# CHECK-EXTENDED-RELOCS:      Characteristics [
# CHECK-EXTENDED-RELOCS-NEXT:   IMAGE_SCN_ALIGN_16BYTES
# CHECK-EXTENDED-RELOCS-NEXT:   IMAGE_SCN_CNT_INITIALIZED_DATA
# CHECK-EXTENDED-RELOCS-NEXT:   IMAGE_SCN_LNK_NRELOC_OVFL
# CHECK-EXTENDED-RELOCS-NEXT:   IMAGE_SCN_MEM_READ
# CHECK-EXTENDED-RELOCS-NEXT: ]
# CHECK-EXTENDED-RELOCS:      Name: .test.section
# CHECK-EXTENDED-RELOCS:      Characteristics [
# CHECK-EXTENDED-RELOCS-NEXT:   IMAGE_SCN_ALIGN_1BYTES
# CHECK-EXTENDED-RELOCS-NEXT:   IMAGE_SCN_CNT_INITIALIZED_DATA
# CHECK-EXTENDED-RELOCS-NEXT: ]
# CHECK-EXTENDED-RELOCS:      SectionData (
# CHECK-EXTENDED-RELOCS-NEXT:  0000: {{.+}}|DEADBEEF{{.+}}|
# CHECK-EXTENDED-RELOCS-NEXT: )

## Test that llvm-objcopy produces an error if the file with section contents
## to be added does not exist.
# RUN: not llvm-objcopy --add-section=.another.section=%t2 %t %t3 2>&1 | FileCheck -DFILE=%t2 -DMSG=%errc_ENOENT %s --check-prefixes=ERR1

# ERR1: error: '[[FILE]]': [[MSG]]

## Another negative test for invalid --add-sections command line argument.
# RUN: not llvm-objcopy --add-section=.another.section %t %t3 2>&1 | FileCheck %s --check-prefixes=ERR2

# ERR2: error: bad format for --add-section: missing '='

## Negative test for invalid --add-sections argument - missing file name.
# RUN: not llvm-objcopy --add-section=.section.name= %t %t3 2>&1 | FileCheck %s --check-prefixes=ERR3

# ERR3: error: bad format for --add-section: missing file name

--- !COFF
header:
  Machine:         IMAGE_FILE_MACHINE_AMD64
  Characteristics: [  ]
sections:
  - Name:            .text
    Characteristics: [  ]
    Alignment:       4
    SectionData:     488B0500000000C3
symbols:
...
